<?php
$todayBegin = strtotime(date('Y-m-d',time()).' 00:00:00');
$todayEnd = $todayBegin + 60 * 60 * 24;

$bookingItem = new BWInputValue($bookingId);
$cartTable = new BWCart();
$bookingRow = new BTreeList($bookingItem);
$cartRow = $cartTable->getListById($cartId);

$venderName = $bookingRow->getItem('venderName')->value;
$courtDisplayName = $bookingRow->getItem('courtDisplayName')->value;
$courtAddress = $bookingRow->getItem('courtAddress')->value;
$courtName = $bookingRow->getItem('courtName')->value;
$courtType = $bookingRow->getItem('courtType')->value;
$courtTypeList = Blyn::app()->getActiveApp()->getCourtTypeList();
$courtType = $courtTypeList[$courtType];
$startTime = $bookingRow->getItem('startTime')->value;
$endTime = $bookingRow->getItem('endTime')->value;
if($bookingRow->getItem('paidTime')){
    $paidTime =$bookingRow->getItem('paidTime')->value; 
}
$price = $cartRow->getItem('price')->value;
$quantity = $cartRow->getItem('quantity')->value;
$total_fee = $price * $quantity;
$wchatSheet = new BSheet('wechatMeta', Blyn::app());
$wchatlist = $wchatSheet->getList(array('itemData'=>array('cartId'=>$cartId)));
$transaction_id = NUll;
if($wchatlist) {
    $transaction_id = $wchatlist->getItem('transaction_id')->getValue();
}
?>
<meta name="viewport" contdent="initial-scale=1, width=device-width, maximum-scale=1, user-scalable=no">
<meta name="viewport" content="initial-scale=1.0,user-scalable=no,maximum-scale=1" media="(device-height: 568px)">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-touch-fullscreen" content="yes">
<meta name="full-screen" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no">
<style type="text/css">
.nav-dropdown ul {margin: 0;}
.nav-dropdown ul li {
    height: 30px;
    line-height: 30px;
}
.table1 {width:100%;line-height:30px;}
.table2 {width:100%;font-size:14px;color:gray;line-height:30px;}
.table1 .l,.table2 .l {text-align:left;}
.table1 .r,.table2 .r {text-align:right;}
.btn-cancel {
    width: 100%;
    height: 30px;
    background-color: #FDB338;
    padding-top: 4px;
}
.btn-cancel:active {background-color: #D8982E;}
</style>
<script type="text/javascript">
$(".nav-dropdown-btn").click(function() {
    $("#nav-dropdown").toggle();
});
</script>
<body>
    <header class="navbar" style="min-height:52px;margin:0px;">
        <table style="width:100%;">
            <tr>
                <td style="width:10%;text-align:left;">
                    <div style="margin-top:8px;margin-left:10px;"><?php
                        echo $control->getAjaxLink(array(
                            'name' => '<img src="'.Yii::app()->request->baseUrl.'/images/mobile/backbtn.png'.'" />', 
                            'id' => 'manageBooking_bookingList'), array(
                            'flagText'=>$flagText,
                            'findText'=>$findText,
                            'htmlOptions' => array(
                            'enctype' => 'multipart/form-data'
                        )));?> 
                    </div>
                </td>
                <td style="width:80%;text-align:center;"><h4 style="margin-top:15px;"><?php echo '流水号：'.$cartId; ?></h4></td>
                <td style="width:10%;text-align:right;">
                    <div class="nav-wrap-right">
                        <a class="react nav-dropdown-btn" style="width:30px;margin-right:8px;">
                            <span class="nav-btn">
                                <i class="text-icon"><img src="<?php echo Yii::app()->request->baseUrl.'/images/mobile/wave.png';?>" /></i>导航
                            </span>
                        </a>
                    </div>           
                    <div id="nav-dropdown" class="nav-dropdown" style="top:52px;width:100px;">
                        <ul>
                            <li><?php
                                echo $control->getAjaxLink(array('name' => '首页', 'id' => 'manageBooking'), array(
                                    'htmlOptions'=>array(
                                    'class'=>'react',
                                    'enctype'=>'multipart/form-data'
                                )));?> 
                            </li>
                        </ul>
                   </div>
                </td>
            </tr>
        </table>
        <div id="ajax-loaderdiv" style="position: fixed;top: 0;left: 0;width: 100%;height: 100%;z-index: 35000;background-color: rgba(0, 0, 0, 0.3);display: none;">
            <img src="/billyn-dev/images/ajax-loader.gif" class="ajax-loader"style="position: absolute;left: 50%;top: 50%;margin-left: -16px;margin-top: -16px;display: block;"/>
        </div>
    </header>
    <dl class="list" style="margin:0px;padding:10px;">
        <dd style="position: relative;">
            <table class="table1" style="width:100%">
                <tr>
                    <td class="l">场地名：</td>
                    <td class="r"><?php echo $courtDisplayName;?></td>
                </tr>
                <tr>
                    <td class="l">场地类型：</td>
                    <td class="r"><?php echo $courtType;?></td>
                </tr>
                <tr>
                    <td class="l">时间：</td>
                    <td class="r"><?php echo date('Y-m-d H:i:s',$startTime).'-'.date('H:i:s',$endTime);?></td>
                </tr>
            </table>
            <hr style="border-color:#dedede;"/>
            <table class="table2" style="width:100%">
                <tr>
                    <td class="l">成交时间：</td>
                    <td class="r"><?php echo date('Y-m-d H:i:s',$paidTime);?></td>
                </tr>
                <tr>
                    <td class="l">预订人：</td>
                    <td class="r">那些年</td>
                </tr>
                <tr>
                    <td class="l">价格：</td>
                    <td class="r" style="color:#FDB338;"><?php echo $price;?>元</td>
                </tr>
                <tr>
                    <td class="l">数量：</td>
                    <td class="r" style="color:#FDB338;"><?php echo $quantity;?></td>
                </tr>
                <tr>
                    <td class="l">支付方式：</td>
                    <td class="r">财付通</td>
                </tr>
                <tr>
                    <td class="l">收款账户：</td>
                    <td class="r">重庆华绿环保发展有限公司</td>
                </tr>
                <tr>
                    <td class="l">当前状态：</td>
                    <td class="r" style="color:#2BB2A3;"><img src="<?php echo Yii::app()->request->baseUrl.'/images/mobile/yes.png';?>" width="20" height="20"/> 已支付</td>
                </tr>
            </table>
            <br/><?php
//            echo $control->getAjaxLink(array('name' => '取消预订', 'id' => 'manageBooking'), array(
//                'htmlOptions' => array(
//                'enctype' => 'multipart/form-data',
//                'class'=>'btn btn-cancel'
//            )));?><?php
            if (Blyn::app()->getUIManager()->getSenderControl()->isLinkClicked('cancelBookingNow_1')) {
                $out_refund_no = "1220336601".time();
                $TotalMoney = $total_fee * 100;//单位换算为元
               /* 商户号 */
                $partner = "1220336601";
                /* 密钥 */
                $key = "89003dcbc62cb9cbdd6a2eb5aded04de";
                /* 创建支付请求对象 */
                $reqHandler = new RequestHandler();
                //通信对象
                $httpClient = new TenpayHttpClient();
                //应答对象
                $resHandler = new ClientResponseHandler();
                //-----------------------------
                //设置请求参数
                //-----------------------------
                $reqHandler->init();
                $reqHandler->setKey($key);
                $reqHandler->setGateUrl("https://mch.tenpay.com/refundapi/gateway/refund.xml");
                $reqHandler->setParameter("partner", $partner);
                $reqHandler->setParameter("transaction_id", $transaction_id);
                //必须保证全局唯一，同个退款单号财付通认为是同笔请求
                $reqHandler->setParameter("out_refund_no", $out_refund_no);//数据更改
                $reqHandler->setParameter("total_fee", $TotalMoney);//数据更改
                $reqHandler->setParameter("refund_fee", $TotalMoney);//数据更改
                $reqHandler->setParameter("op_user_id", "1220336601");
                //操作员密码,MD5处理
                $reqHandler->setParameter("op_user_passwd", md5("hl0701!"));
                //接口版本号,取值1.1
                $reqHandler->setParameter("service_version", "1.1");
                //-----------------------------
                //设置通信参数
                //-----------------------------
                //设置PEM证书，pfx证书转pem方法：openssl pkcs12 -in 2000000501.pfx  -out 2000000501.pem
                //证书必须放在用户下载不到的目录，避免证书被盗取
                $httpClient->setCertInfo( yii::getPathOfAlias('application.application.app.BWeSport.classes').'\1220336601_20140804170504.pem', "1220336601");
                //设置CA
                $httpClient->setCaInfo( yii::getPathOfAlias('application.application.app.BWeSport.classes').'\cacert.pem');
                $httpClient->setTimeOut(5);
                //设置请求内容
                $httpClient->setReqContent($reqHandler->getRequestURL());
                if($httpClient->call()) {
                    //设置结果参数
                    $resHandler->setContent($httpClient->getResContent());
                    $resHandler->setKey($key);

                    //判断签名及结果
                    //只有签名正确并且retcode为0才是请求成功
                    if($resHandler->isTenpaySign() && $resHandler->getParameter("retcode") == "0" ) {
                        //取结果参数做业务处理
                        //商户订单号
                        $out_trade_no = $resHandler->getParameter("out_trade_no");	
                        //财付通订单号
                        $transaction_id = $resHandler->getParameter("transaction_id");	
                        //商户退款单号
                        $out_refund_no = $resHandler->getParameter("out_refund_no");	
                        //财付通退款单号
                        $refund_id = $resHandler->getParameter("refund_id");	
                        //退款金额,以分为单位
                        $refund_fee = $resHandler->getParameter("refund_fee");		
                        //退款状态``````````````````````````````````````````````````````` 
                        $refund_status = $resHandler->getParameter("refund_status");
                        if($refund_status == 4 || $refund_status == 8|| $refund_status == 9|| $refund_status == 10|| $refund_status == 11){ ?>
                            <p>你已经取消了你的预订！</p>
                            <p>取消的订单号：<?php echo $bookingId; ?></p>
                            <p>返回订金：<?php echo $price * $quantity; ?> 元</p>
                            <p>进入我的已支付订单记录查看结果！</p> 
                            <p>需要1--3个工作日，资金可返回到您的账户！</p><?php 
                        } else {?>
                            <p>退款失败！</p><?php
                        }?><?php
//                         echo "OK,refund_status=" . $refund_status . ",out_refund_no=" . $resHandler->getParameter("out_refund_no") . ",refund_fee=" . $resHandler->getParameter("refund_fee") . "<br>";
                    } else  {
                        //错误时，返回结果可能没有签名，记录retcode、retmsg看失败详情。
                        echo "验证签名失败 或 业务错误信息:retcode=" . $resHandler->getParameter("retcode"). ",retmsg=" . $resHandler->getParameter("retmsg") . "<br>";
                    }
                } else {
                        echo "后台调用通信失败，请联系管理员";
//                        echo "call err:" . $httpClient->getResponseCode() ."," . $httpClient->getErrInfo() . "<br>";
                        //有可能因为网络原因，请求已经处理，但未收到应答。
                }
            } else {
                if($transaction_id){// 如果transaction_id 不为NULL
                    $cancelPaidUrl = $control->getLinkUrl(array('name' => 'cancelBooking','id' => 'cancelBookingNow_1'), array(
                    'cartId' => $cartId,
                    'total_fee' => $total_fee,
                    'transaction_id' => $transaction_id,
                    'bookingId' => $bookingId));
        //           echo '<a class="btn" href="http://www.hualvhb.cn/pay/weixin/Refund/clientRefund.php?pUrl=http://stage.billyn.net'.$cancelPaidUrl.'&tI='.$transaction_id.'&tF='.$total_fee.'">确定</a>';
        //            echo $transaction_id; 
                    echo '<a class="btn" href="'.$cancelPaidUrl.'">确定</a>';
                }  else {
//                    $findUrl = $control->getLinkUrl(array('name' => 'find','id' => 'booking_find_1'));
                    echo '<a class="btn btn-cancel" href="#">不能退款</a>';
                }
        //            echo '<a class="btn" href="'.$cancelPaidUrl.'">确定</a>';
        //        echo $control->getLink(array('name' => '确认', 'id' => 'cancelBookingNow_1'), array(
        //            'cartId' => $cartId,
        //            'bookingId' => $bookingId,
        //            'htmlOptions' => array('class' => 'btn')));
            }?>
            <br/><br/>
        </dd>
    </dl>
    <footer>
        <div class="footer-copyright">
            <div class="hr"></div>
            <span class="footer-copyright-text">
                Copyright © 2014 billyn.net </span>
        </div>
    </footer>
</body>